package com.linlang.grasharepro.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.linlang.grasharepro.R;
import com.linlang.grasharepro.adapter.BiYeShareAdapter;
import com.linlang.grasharepro.dao.FansDB;
import com.linlang.grasharepro.dao.FollowDB;
import com.linlang.grasharepro.dao.NewMessageDB;
import com.linlang.grasharepro.dao.ShareCoentenDB;
import com.linlang.grasharepro.dao.ShareMessageDB;
import com.linlang.grasharepro.model.Fans;
import com.linlang.grasharepro.model.Follow;
import com.linlang.grasharepro.model.NewMessageDomain;
import com.linlang.grasharepro.model.ShareCoenten;
import com.linlang.grasharepro.utils.FileUtiles;
import com.linlang.grasharepro.utils.ListViewUitls;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 软件启动界面
 */
public class UserInfoActivity extends Activity {

    //分享的LISTVIEW
    private ListView shareListView;
    //分享的LIST变量
    private List<ShareCoenten> share_list = new ArrayList<ShareCoenten>();
    private ShareCoenten info = null;
    private LinearLayout ll_bottom, user_info_sendmessage, user_info_addattention;
    private TextView userName, sex, profe, title, content, slef_release, slef_love, slef_attention,tv_introduction;
    private ImageView head, type, user_info_back;

    private ShareCoentenDB shareCoentenDomainDB;
    private FollowDB followDB;
    private FansDB fansDB;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.user_info_detail);

        // 获取从列表当中传递过来的数据
        Intent intent = getIntent();
        Bundle bund = intent.getExtras();
        info = (ShareCoenten) bund.getSerializable("shareContent");

        initView();
        initUserShare();

        //后退按钮
        user_info_back = (ImageView) findViewById(R.id.user_info_back);
        user_info_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UserInfoActivity.this.finish();
            }
        });
    }

    ;

    /**
     * 初始化分享信息
     */
    public void initUserShare() {

        shareCoentenDomainDB = new ShareCoentenDB(this);
        shareCoentenDomainDB.open();
        share_list = shareCoentenDomainDB.queryByWhere(info.getUserName());

        this.shareListView = (ListView) this.findViewById(R.id.user_info_messageList);
        final BiYeShareAdapter shareAdapter = new BiYeShareAdapter(share_list, this);
        shareListView.setAdapter(shareAdapter);
        ListViewUitls.setListViewHeightBasedOnChildren(shareListView);
        //设置单击事件
        shareListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                Toast.makeText(UserInfoActivity.this, share_list.get(position).toString(), Toast.LENGTH_LONG).show();
            }
        });


        ArrayList<ShareCoenten> shareCoentens = shareCoentenDomainDB.queryByWhere(info.getUserName());

        fansDB = new FansDB(this);
        fansDB.open();
        ArrayList<Fans> fanses = fansDB.getMyFans(info.getUserName());

        followDB = new FollowDB(this);
        followDB.open();
        ArrayList<Follow> follows = followDB.getMyFollow(info.getUserName());

        slef_release.setText("发布(" + shareCoentens.size() + ")");
        slef_love.setText("粉丝(" + fanses.size() + ")");
        slef_attention.setText("关注(" + follows.size() + ")");
    }

    private void initView() {


        //如果是自己进入，则隐藏关注和私信
        //获取用户登陆信息
        SharedPreferences preferences = getSharedPreferences("userInfo",
                Activity.MODE_PRIVATE);
        String account = preferences.getString("account", "");
        String introduction = preferences.getString("introduction", "");
        String mysex = preferences.getString("sex", "");

        if (info.getUserName().equals(account)) {
            ll_bottom = (LinearLayout) findViewById(R.id.ll_bottom);
            ll_bottom.setVisibility(View.GONE);
        }


        userName = (TextView) findViewById(R.id.detail_user);
        type = (ImageView) findViewById(R.id.detail_type);
        profe = (TextView) findViewById(R.id.detail_profe);
        sex = (TextView) findViewById(R.id.detail_sex);
        head = (ImageView) findViewById(R.id.detail_head);
        tv_introduction = (TextView) findViewById(R.id.tv_introduction);

        userName.setText(info.getUserName());
        profe.setText(info.getMajor());
        sex.setText(mysex);
        tv_introduction.setText(introduction);

        if (info.getType().equals("在校生"))
            type.setImageResource(R.drawable.inschool);
        else
            type.setImageResource(R.drawable.outschool);

        head.setVisibility(View.VISIBLE);
        Bitmap bitmap0 = FileUtiles.getSmallBitmap(info.getHeadUrl());
        head.setImageBitmap(bitmap0);



        user_info_sendmessage = (LinearLayout) findViewById(R.id.user_info_sendmessage);//私信
        user_info_addattention = (LinearLayout) findViewById(R.id.user_info_addattention);//关注
        slef_release = (TextView) findViewById(R.id.slef_release);//我发布的
        slef_love = (TextView) findViewById(R.id.slef_love);//我的粉丝
        slef_attention = (TextView) findViewById(R.id.slef_attention);//我的关注


        followDB = new FollowDB(UserInfoActivity.this);
        followDB.open();

        //如果已关注则隐藏关注按钮
        ArrayList<Follow> follows = followDB.getMyFollow(account, info.get_id());
        if (follows.size() != 0) {
            user_info_addattention.setVisibility(View.GONE);
        }


        user_info_sendmessage.setOnClickListener(new MyOnClickListener(1));
        user_info_addattention.setOnClickListener(new MyOnClickListener(2));
        slef_release.setOnClickListener(new MyOnClickListener(3));
        slef_love.setOnClickListener(new MyOnClickListener(4));
        slef_attention.setOnClickListener(new MyOnClickListener(5));
    }

    public class MyOnClickListener implements View.OnClickListener {
        private int index = 0;

        public MyOnClickListener(int i) {
            index = i;
        }

        //获取用户登陆信息
        SharedPreferences preferences = getSharedPreferences("userInfo",
                Activity.MODE_PRIVATE);
        String account = preferences.getString("account", "");
        String id = preferences.getString("id", "");
        String type = preferences.getString("type", "");
        String major = preferences.getString("major", "");
        String headUrl = preferences.getString("headUrl", "");
        String dateStr = "";
        Intent intent = null;
        Bundle bundle = null;

        @Override
        public void onClick(View v) {
            switch (index) {
                case 1://私信
                    bundle = new Bundle();
                    bundle.putSerializable("shareContent", info);
                    intent = new Intent(UserInfoActivity.this,
                            MessageActivity.class);
                    intent.putExtras(bundle);

                    break;
                case 2://关注
                    followDB = new FollowDB(UserInfoActivity.this);
                    followDB.open();

                    try {
                        Date date = new Date();
                        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
                        dateStr = simpleDateFormat.format(date);
                    } catch (Exception e) {
                    }

                    Follow follow = new Follow();
                    follow.setUserName(account);
                    follow.setRelationId(info.get_id());
                    follow.setTime(dateStr);

                    followDB.insertFollow(follow);

                    //新增粉丝表
                    FansDB fansDB = new FansDB(UserInfoActivity.this);
                    fansDB.open();
                    Fans fans = new Fans();
                    fans.setRelationId(id);
                    fans.setUserName(info.getUserName());
                    fans.setTime(dateStr);
                    fansDB.insertFans(fans);

                    //新增新消息表


                    break;
                case 3:
                    bundle = new Bundle();
                    bundle.putSerializable("shareContent", info);
                    intent = new Intent(UserInfoActivity.this,
                            MyShareActivity.class);
                    intent.putExtras(bundle);
                    break;
                case 4:
                    bundle = new Bundle();
                    bundle.putSerializable("shareContent", info);
                    intent = new Intent(UserInfoActivity.this,
                            MyFansActivity.class);
                    intent.putExtras(bundle);
                    break;
                case 5:
                    bundle = new Bundle();
                    bundle.putSerializable("shareContent", info);
                    intent = new Intent(UserInfoActivity.this,
                            MyFollowActivity.class);
                    intent.putExtras(bundle);
                    break;
            }
            if (index != 2)
                startActivity(intent);
//			if(index == 1){
//				Intent intent = new Intent(UserInfoActivity.this,
//						MessageActivity.class);
//				startActivity(intent);
//			}
        }
    }
}
